﻿// -----------------------------------------------------------------------------
// DM 是 .NET 5 平台下开发管理的系统。
// Copyright © 2021 DM,  Co.,Ltd.
//
// 框架名称：Furion
// 后台框架：ABC.Admin
// -----------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tool
{
    public static class PearMenuExtensions
    {

        /// <summary>
        /// 获取Pear 后台菜单
        /// </summary>
        /// <param name="menus"></param>
        /// <param name="pid">父级ID</param>
        /// <returns></returns>
        public static List<PearMenuItem> GetMenus( this List<PearMenuInput> menus, string pid="")
        {
            List<PearMenuItem> menuItems = new List<PearMenuItem>();
            foreach (var menu in menus.Where(o => o.PMId == pid).OrderBy(o => o.Sort))
            {
                //var type = menus.Any(o => o.ParentId == menu.Id) ? 0 : 1;
                if (menu.MenuType == MenuType.目录) //目录
                {
                    PearMenuItem menuitem = new PearMenuItem()
                    {
                        id = menu.MId,
                        title = menu.Name,
                        icon = menu.Icon,
                        type = (int)menu.MenuType,
                    };
                    if (!string.IsNullOrWhiteSpace(menu.Url)) menuitem.href = $"/{menu.Url.TrimStart('/')}";

                    menuitem.children = GetMenus(menus, menu.MId);
                    menuItems.Add(menuitem);
                }
                else
                {
                    PearMenuItem menuitem = new PearMenuItem()
                    {
                        id = menu.MId,
                        title = menu.Name,
                        icon = menu.Icon,
                        type = (int)menu.MenuType,
                        openType = "_iframe"
                    };
                    if (!string.IsNullOrWhiteSpace(menu.Url)) menuitem.href = $"/{menu.Url.TrimStart('/')}";
                    menuItems.Add(menuitem);
                }

                //}
            }

            return menuItems;
        }
    }

    public class PearMenuInput
    {

        /// <summary>
        /// 节点类型(菜单、工具栏)
        /// </summary>
        public MenuType MenuType { get; set; }
        /// <summary>
        /// 菜单ID，全局唯一标识
        /// </summary>
        public string MId { get; set; }

        /// <summary>
        /// 父Id
        /// </summary>
        public string? PMId { get; set; }

        /// <summary>
        /// 菜单名称
        /// </summary>
        public string Name { get; set; }



        /// <summary>
        /// 图标
        /// </summary>
        public string Icon { get; set; }

        /// <summary>
        /// 顺序(按从小到大排序)
        /// </summary>
        public int Sort { get; set; }

        /// <summary>
        /// Url
        /// </summary>
        public string Url { get; set; }
    }
    public enum MenuType
    {
        /// <summary>
        /// 目录
        /// </summary>
        目录 = 0,

        /// <summary>
        /// 菜单
        /// </summary>
        菜单 = 1,

        /// <summary>
        /// API 接口
        /// </summary>
        API = 2,
    }
}
